﻿namespace GreenIsomer.Web.Controls.JQuery {
	using System;
	using System.Web.UI;
	using System.Web.UI.WebControls;
	using System.ComponentModel;

	/// <summary>
	/// Server control used to generate a static jQuery UI Themed panel.
	/// </summary>
	[ParseChildren(false), PersistChildren(true)]
	public class ThemedPanel : JQueryWebControl, IThemed {


		#region Constants: Property defaults

		private const PanelType defaultPanelType = PanelType.Content;
		private const bool defaultHighlight = false;
		private const bool defaultShowErrorState = false;
		private const Priority defaultPriority = Priority.None;

		#endregion


		#region Constructors

		/// <summary>
		/// Initializes a new instance of the GreenIsomer.Web.Controls.JQuery.ThemedPanel class.
		/// </summary>
		public ThemedPanel() : base(HtmlTextWriterTag.Div) { }

		#endregion


		#region Properties: Theme

		/// <summary>
		/// Gets or sets a value representing logical jQuery UI component containers defined by the theme framework.
		/// </summary>
		/// <exception cref="ArgumentException">Value not defined.</exception>
		[DefaultValue(defaultPanelType), Category("Appearance")]
		public PanelType PanelType {
			get { return ViewState.GetValue("PanelType", defaultPanelType); }
			set {
				if(!Enum.IsDefined(typeof(PanelType), value)) throw new ArgumentException("Value not defined.", "value");

				ViewState["PanelType"] = value;
			}
		}

		/// <summary>
		/// Gets or sets whether or not the control should be rendered with a highlighted interaction cue defined by the theme framework.
		/// </summary>
		[DefaultValue(defaultHighlight), Category("Appearance")]
		public virtual bool Highlight {
			get { return ViewState.GetValue("Highlight", defaultHighlight); }
			set { ViewState["Highlight"] = value; }
		}

		/// <summary>
		/// Gets or sets whether or not the control should be rendered with an error interaction cue defined by the theme framework.
		/// </summary>
		[DefaultValue(defaultShowErrorState), Category("Appearance")]
		public virtual bool ShowErrorState {
			get { return ViewState.GetValue("ShowErrorState", defaultShowErrorState); }
			set { ViewState["ShowErrorState"] = value; }
		}

		/// <summary>
		/// Gets or sets a value representing priority-based interaction cues defined by the theme framework.
		/// </summary>
		/// <exception cref="ArgumentException">Value not defined.</exception>
		[DefaultValue(defaultPriority), Category("Appearance")]
		public virtual Priority Priority {
			get { return ViewState.GetValue("Priority", defaultPriority); }
			set {
				if(!Enum.IsDefined(typeof(Priority), value)) throw new ArgumentException("Value not defined.", "value");

				ViewState["Priority"] = value;
			}
		}

		#endregion


		#region Properties: Protected explicit implementation

		/// <summary>
		/// Gets whether or not the control should be rendered with jQuery UI theme classes.
		/// </summary>
		protected virtual bool UseTheme {
			get { return true; }
		}

		#endregion


		#region Methods: Rendering hooks

		/// <summary>
		/// Allows a jQuery server control to reference scripts and stylesheets necessary to render their client markup and widgets.
		/// </summary>
		/// <param name="manager">The GreenIsomer.Web.Controls.JQuery.Manager used to handle style and script referencing in addition to unified generation of load and ready scripts.</param>
		protected override void Register(Manager manager) {
			manager.RegisterStyleFile(Settings.Default.UiCoreStyleFile);
			manager.RegisterThemeStyleFile();
		}

		/// <summary>
		/// Sends server control content to a provided System.Web.UI.HtmlTextWriter object, which writes the content to be rendered to the client.
		/// This method is used primarily by control developers.
		/// </summary>
		/// <param name="writer">A System.Web.UI.HtmlTextWriter that represents the output stream to render HTML content on the client.</param>
		protected override void Render(HtmlTextWriter writer) {

			if(!MasterUseCssStyling)
				writer.AddStyleAttribute(HtmlTextWriterStyle.Padding, "6px");

			string originalCssClass = CssClass;
			CssClass = this.GetThemedCssClass();
	
			base.Render(writer);

			CssClass = originalCssClass;
		}

		#endregion


		#region Explicit Implementation: IThemed

		bool IThemed.UseTheme {
			get { return UseTheme; }
		}

		#endregion


	}

}